import axios from 'axios'
import { GetCookies, RemoveCookies } from './cookie'
import { InitWxLogin } from '.'
import { NANCH5COOKIEKEY, NANCH5HEADER } from '../constants'
const Axios = axios.create({
  withCredentials: true
})

Axios.interceptors.request.use((req) => {
  // 请求时带上 uuid
  req.headers[NANCH5HEADER] = GetCookies(NANCH5COOKIEKEY) || ''
  return req
})

Axios.interceptors.response.use((res) => {
  // 当接口不正常移除cookie里的uuid
  if (res && res.data && res.data.code === 202) {
    RemoveCookies(NANCH5COOKIEKEY)
    InitWxLogin()
  }
  return res || { data: { code: -1, msg: '服务器错误' }}
})

export default Axios
